package sit.mycatalog.web.mbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpServletRequest;

import org.richfaces.event.ItemChangeEvent;

import sit.mycatalog.ejb.sess.CatnodeSess;
import sit.mycatalog.ejb.sess.ItemSess;
import sit.mycatalog.jpa.Catnode;
import sit.mycatalog.jpa.Item;
import sit.mycatalog.util.SessLookup;

public class CatalogMenuBean implements Serializable {
	private static final long serialVersionUID = 8255718160466009714L;
	private List<Catnode> catnode;
	private String resultTitle;
//	private String keyword;
	private String keyworddesc;
	private Double keywordfromprice;
	private Double keywordtoprice;
	private Double keywordheight;
	private Double keywordlength;
	private Double keywordepth;
	private Integer rows;

	private String current;

	private boolean singleMode;

	private List<Item> itemlist;

	public String getCurrent() {
		return current;
	}

	public void setCurrent(String current) {
		this.current = current;
	}

	public boolean isSingleMode() {
		return singleMode;
	}

	public void setSingleMode(boolean singleMode) {
		this.singleMode = singleMode;
	}

	public List<Catnode> getCatnode() {
		return catnode;
	}

	public void setCatnode(List<Catnode> catnode) {
		this.catnode = catnode;
	}

	public List<Item> getItemlist() {
		return itemlist;
	}

	public void setItemlist(List<Item> itemlist) {
		this.itemlist = itemlist;
	}

	public String getResultTitle() {
		return resultTitle;
	}

	public void setResultTitle(String resultTitle) {
		this.resultTitle = resultTitle;
	}
	
//	public String getKeyword() {
//		return keyword;
//	}
//
//	public void setKeyword(String keyword) {
//		this.keyword = keyword;
//	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}	

	public String getKeyworddesc() {
		return keyworddesc;
	}

	public void setKeyworddesc(String keyworddesc) {
		this.keyworddesc = keyworddesc;
	}

	public Double getKeywordheight() {
		return keywordheight;
	}

	public void setKeywordheight(Double keywordheight) {
		this.keywordheight = keywordheight;
	}

	public Double getKeywordlength() {
		return keywordlength;
	}

	public void setKeywordlength(Double keywordlength) {
		this.keywordlength = keywordlength;
	}

	public Double getKeywordepth() {
		return keywordepth;
	}

	public void setKeywordepth(Double keywordepth) {
		this.keywordepth = keywordepth;
	}

	public Double getKeywordfromprice() {
		return keywordfromprice;
	}

	public void setKeywordfromprice(Double keywordfromprice) {
		this.keywordfromprice = keywordfromprice;
	}

	public Double getKeywordtoprice() {
		return keywordtoprice;
	}

	public void setKeywordtoprice(Double keywordtoprice) {
		this.keywordtoprice = keywordtoprice;
	}

	@PostConstruct
	public void init() {
		try {
			CatnodeSess sessCatnode = SessLookup.getCatnodeSess();
			Catnode entCatnode = sessCatnode.getCatnode(new Integer(0));
			entCatnode = sessCatnode.getAllChild(entCatnode);

			catnode = entCatnode.getChildNode();
			
			loadHotItem();
			
			rows = new Integer(9);
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
	}

	public void updateCurrent(ItemChangeEvent event) {
		if ((event.getNewItemName() != null) && (event.getNewItemName().length() > 0)) {
			setCurrent(event.getNewItemName());
		}
	}

	public void selectMenuItem(ActionEvent event) {
		try {
			Integer intCatnode = null;
			
			try {
				intCatnode = Integer.parseInt(current);
			} catch (Exception e) {
				e.printStackTrace(System.out);
			}
			
			if (intCatnode != null) {
				CatnodeSess sessCatnode = SessLookup.getCatnodeSess();
				Catnode entCatnode = sessCatnode.getCatnode(intCatnode);
				resultTitle = entCatnode.getName();
//				keyword = "";
				keyworddesc = "";
				keywordfromprice = null;
				keywordtoprice = null;
				keywordheight = null;
				keywordlength = null;
				keywordepth = null;
				
				ItemSess sessItem = SessLookup.getItemSess();
				itemlist = sessItem.getByCatid(entCatnode.getCatid());
			}
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}
	
	public void search(AjaxBehaviorEvent event) {
		try {
//			Map<String, Object> m = event.getComponent().getAttributes();
//			Set s = m.keySet();
//			Iterator itr = s.iterator();
//			
//			while(itr.hasNext()) {
//				System.out.println(itr.next());
//			}
			
			ItemSess sessItem = SessLookup.getItemSess();
			HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
//			System.out.println(request.getParameter("catalogForm:keyword"));
			resultTitle = "Search Results";
			
			if(keywordfromprice == 0) {
				keywordfromprice = null;
			}
			
			if(keywordtoprice == 0) {
				keywordtoprice = null;
			}
			
			if(keywordheight == 0) {
				keywordheight = null;
			}
			
			if(keywordlength == 0) {
				keywordlength = null;
			}
			
			if(keywordepth == 0) {
				keywordepth = null;
			}
			
			itemlist = sessItem.searchItem(keyworddesc, keywordfromprice, keywordtoprice, keywordheight, keywordlength, keywordepth);
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}
	
	public void searchHotItem(ActionEvent even) {
		loadHotItem();
	}
	
	public void loadHotItem() {
		try {
			ItemSess sessItem = SessLookup.getItemSess();
			resultTitle = "Hot Items";
			itemlist = sessItem.getHotItem();
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}
	
	public void changeRows(AjaxBehaviorEvent event) {
	}
	
	public void clearsearch(AjaxBehaviorEvent event) {
		keyworddesc = "";
		keywordfromprice = null;
		keywordtoprice = null;
		keywordheight = null;
		keywordlength = null;
		keywordepth = null;
		
		itemlist = new ArrayList<Item>();
	}
}
